package com.sporadic.desk.service.impl;

import bo.KeepOrderDetailsBO;
import bo.QueueUseObjet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sporadic.desk.dao.OrderDetailsDao;
import com.sporadic.desk.pojo.Order;
import com.sporadic.desk.pojo.OrderDetails;
import com.sporadic.desk.service.OrderDetailsService;
import com.sporadic.desk.service.OrderService;
import com.sporadic.desk.service.QueueService;
import dto.OrderDetailsDTO;
import dto.UserInfoDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @program: sporadic
 * @description:
 * @author: LHD   QQ:1727865856
 * @create: 2019-10-24 10:15
 **/
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsDao, OrderDetails> implements OrderDetailsService {
    @Autowired
    private OrderService orderService;
    @Autowired
    private QueueService queueService;

    @Override
    public boolean addOrderDetails(OrderDetails orderDetails) {
        return super.save(orderDetails);
    }

    @Transactional
    @Override
    public boolean keepOrderDetails(KeepOrderDetailsBO keepOrderDetailsBO) {
        //更新订单总金额x
        Order oldorder = orderService.getById(keepOrderDetailsBO.getOrderId());
        //add()为BigDecimal的相加方法
        BigDecimal newMoney = oldorder.getOrderTotal().add(keepOrderDetailsBO.getSumPrice());
        oldorder.setOrderPrice(newMoney);
        oldorder.setOrderTotal(newMoney);
        oldorder.setOrderUpdateTime(new Date());
        try {
            orderService.update(oldorder,
                    new QueryWrapper<Order>()
                            .eq("order_id", keepOrderDetailsBO.getOrderId()));
            //将List<keepOrderDetailsBO>类型的集合复制到List<OrderDetails>中进行保存
            List<OrderDetails> list = new ArrayList<>();
            keepOrderDetailsBO.getOrderdetails().forEach(order -> {
                OrderDetails orderDetails = new OrderDetails();
                order.setOdOrderId(keepOrderDetailsBO.getOrderId());
                BeanUtils.copyProperties(order, orderDetails);
                list.add(orderDetails);
            });
            super.saveBatch(list);
            queueService.addMessage(new QueueUseObjet(keepOrderDetailsBO.getOrderdetails()
                    ,new UserInfoDTO(oldorder.getOrderMerchantId())
                    ,oldorder.getOrderDesk()));
            return true;
        } catch (Exception e) {
            throw new RuntimeException("添加失败！");
        }
    }

    @Transactional
    @Override
    public boolean delOrderDetails(Integer detailId) {
        try {
            BigDecimal sumPrice = BigDecimal.ZERO;
            //先查询然后删除一个菜品
            OrderDetails details = getById(detailId);
            super.removeById(detailId);
            //重新计算价格
            List<OrderDetails> orderDetails = list(new QueryWrapper<OrderDetails>()
                    .eq("od_orderId", details.getOdOrderId()));
            for (OrderDetails orderDetail : orderDetails) {
                BigDecimal price = orderDetail.getOdDishesPrice().multiply(new BigDecimal(orderDetail.getOdDishesNums()));
                System.out.println(price);
                sumPrice = sumPrice.add(price);
            }
            // 更新订单价格
            Order order = orderService.getById(details.getOdOrderId());
            order.setOrderTotal(sumPrice);
            order.setOrderUpdateTime(new Date());
            orderService.update(order, new QueryWrapper<Order>().eq("order_id", order.getOrderId()));
            //退单成功向后厨发送，退单小票
            OrderDetailsDTO orderDetailsDTO=new OrderDetailsDTO();
            BeanUtils.copyProperties(details,orderDetailsDTO);
            queueService.addMessage(new QueueUseObjet(Arrays.asList(orderDetailsDTO)
                    ,new UserInfoDTO(order.getOrderMerchantId())
                    ,order.getOrderDesk(),0));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("删除失败!");
        }
        return true;
    }
}
